<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form"  :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="类型选择">
              <el-select v-model="form.type" placeholder="请选择" clearable>
                <el-option v-for="item in typeList" :key="item.type" :label="item.name" :value="item.type" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="省份">
              <el-select v-model="form.province" placeholder="请选择" clearable>
                <el-option v-for="item in options" :key="item.path" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标题">
              <el-input v-model="form.title" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button v-if="hasPermission('AdminMaterialDistributionProvider.saveMaterialDistribution')" type="primary" @click="toRoutes('materialPlaceAdd')">新增</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="title" label="标题" />
        <el-table-column label="封面图">
          <template slot-scope="scope">
            <div>
              <img style="height:40px;width:40px" :src="scope.row.miniUrl | imgUrlFn" alt="">
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="province" label="发放省份" />
        <el-table-column prop="isRecom" label="是否推荐" />
        <el-table-column prop="status" label="状态" />
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="createdAt" label="添加时间" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showPic(scope.row.miniUrl)">查看海报</el-button>
            <el-button v-if="hasPermission('AdminMaterialDistributionProvider.updateMaterialDistribution')" type="text" size="small" @click="toRoutes('materialPlaceEdit',scope.row.id)">编辑</el-button>
            <el-button v-if="hasPermission('AdminMaterialDistributionProvider.deleteMaterialDistributionById')" type="text" size="small" @click="deleteList(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  materialPlaceList,
  materialPlaceListDelete,
  materialtypeList,
  listMaterialDistributionsByPage
} from '@/api/materialPlace'
import { policy } from '@/api/policy'
import { regionDropDown } from '@/api/area'
export default {
  name: 'MaterialPlaceIndex',
  filters: {
    imgUrlFn: function(e) {
      return localStorage.getItem('imgUrlPrefix') + e
    }
  },
  data() {
    return {
      tableLoading: true,
      form: {
        pageNum: 1,
        pageSize: 10,
        title: '',
        type: null,
        province: null
      },
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      },
      options: [],
      typeList: []
    }
  },
  mounted() {    
    this.getArea()
    this.getTypeList()
    this.getList(this.listForm)
  },
  methods: {
    // 按钮权限
    hasPermission(e){
        return window.helper.hasPermission(e)
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + '成功',
        type: 'success'
      })
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + '失败')
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1
      // console.log(this.form)
      this.getList(this.form)
    },
    //获取类型下拉列表
    getTypeList(){
      let datas = {}
      materialtypeList(datas).then(res=>{
        this.typeList = res.data.data
      })
    },
    // 省市区
    getArea() {
      regionDropDown().then(res => {
        const newArr = []
        res.data.data.forEach((item1, index) => {
          newArr.push({
            value: item1.id,
            label: item1.name
          })
        })
        this.options = newArr
      })
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true
      const datas = e
      if (datas.type == '') {
        datas.type = null
      } else if (datas.province == '') {
        datas.province = null
      }
      listMaterialDistributionsByPage(datas)
        .then(res => {
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list
            this.page_totals = res.data.data.count
            if (this.tableData.length > 0) {
              this.tableData.forEach(item => {
                item.isRecom = item.isRecom == 0 ? '不推荐' : '推荐'
                item.status = item.status == 0 ? '正常' : '下架'
                this.options.forEach(list => {
                  if (item.province == list.value) {
                    item.province = list.label
                  }
                })
              })
            }
          } else {
            this.$message.error(res.data.msg)
          }
          this.tableLoading = false
        })
        .catch(error => {
          this.$message.error(error)
        })
    },
    // 路由跳转
    toRoutes(e, id) {
      const route = { name: e, query: { id: id }}
      this.$router.push(route)
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val
      this.getList(this.form)
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val
      this.getList(this.form)
    },
    // 删除
    deleteList(e) {
      this.$confirm('删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let datas = {id:e}
          materialPlaceListDelete(datas).then(res => {
            if (res.data.code == 0) {
              this.succStatus('删除')
              this.getList(this.form)
            } else {
              this.$message.error(res.data.msg)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    showPic(img) {
      const imgUrl = localStorage.getItem('imgUrlPrefix') + img
      window.open(imgUrl)
    }
  }
}
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
